package com.lanou.musicproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.MainActivity;
import com.lanou.musicproject.activity.MusicPlayActivity;
import com.lanou.musicproject.bean.MoreSongInfo;
import com.lanou.musicproject.bean.MyMusicList;
import com.lanou.musicproject.bean.NewSong;
import com.lanou.musicproject.suspendservice.FxService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 王世美 on 15/10/28.
 * 最新歌曲详细内容
 */
public class MoreSongInfoAdapter extends BaseAdapter {
    // 定义一个实体类数组
    private List<MoreSongInfo.SonglistEntity> songList;
    // 上下文
    private Context context;

    //有参构造方法
    public MoreSongInfoAdapter(Context c, List<MoreSongInfo.SonglistEntity> songList) {
        this.songList = songList;
        context = c;
    }

    //获得行布局个数
    @Override
    public int getCount() {
        if (songList != null && songList.size() > 0)
            return songList.size();
        else
            return 0;
    }

    // 获得行布局坐标
    @Override
    public MoreSongInfo.SonglistEntity getItem(int position) {
        if (songList != null && songList.size() > 0)
            return songList.get(position);
        else
            return null;
    }

    // 获得行布局内容Id
    @Override
    public long getItemId(int position) {
        return position;
    }

    // 获得行布局内容
    @Override
    public View getView(final int position,
                        View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.rotate_info_listview_item, null);
            holder = new ViewHolder();
            holder.rorateTitleTV = (TextView) convertView.findViewById(R.id.rorateTitleTV);
            holder.rorateAuthorTV = (TextView) convertView.findViewById(R.id.rorateAuthorTV);
            holder.rotate_listView = (LinearLayout) convertView.findViewById(R.id.rotate_listView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
       MoreSongInfo.SonglistEntity songlistEntity = getItem(position);
        if (songlistEntity != null) {
            holder.rorateTitleTV.setText(songlistEntity.getTitle());
            holder.rorateAuthorTV.setText(songlistEntity.getAuthor());
            holder.rotate_listView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<MyMusicList> lists = new ArrayList<>();
                    for (MoreSongInfo.SonglistEntity song : songList) {
                        MyMusicList musicList = new MyMusicList();
                        musicList.setName(song.getTitle());
                        musicList.setSinger(song.getAuthor());
                        musicList.setSongId(song.getSong_id());
                        musicList.setPlayer_singer_image(song.getPic_big());
                        musicList.setFrom("网络");
                        lists.add(musicList);
                    }
                    MainActivity.mini_play(lists, position);

                    FxService.mini_play(lists, position);
                    MusicPlayActivity.playIv(lists, position);

                }

            });

        }
        return convertView;
    }

    // 初始化组件
    private class ViewHolder {
        public TextView rorateTitleTV;
        public TextView rorateAuthorTV;
        public LinearLayout rotate_listView;
    }
}
